//
// C++ Implementation: snapshotmanager
//
// Description: 
//
//
// Author:  <>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//


#include "snapshotmanager.h"
#include <libwormhole/snapshot.h>
#include <libwormhole/backendinterface.h>
#include <kservicetype.h>
#include <kservicetypetrader.h>
#include <kdebug.h>
#include <QDebug>
#include <QDir>
#include <QCoreApplication>
#include <kmessagebox.h>
SnapshotManager::SnapshotManager()
 : QObject()
{
loadBackends();
}


SnapshotManager::~SnapshotManager()
{
}

bool SnapshotManager::executeSnapshot(Snapshot *s)
{
	QString backendname=s->backendname();
	BackendInterface *currbackend=m_backendhash[s->backendname()];

	if(!currbackend)
		return false;

	/*
		TODO: do snapshot
	*/
      currbackend->doSnapshot(s);
	return true;
	
}
BackendInterface *SnapshotManager::createBackend(KService::Ptr newService)
{
BackendInterface *backend=0;
qDebug() << newService->library();
    if (0 == backend) {
        const QLatin1String suffix("/wormhole_backend/");
        const QStringList libFilter(newService->library() + QLatin1String(".*"));
	qDebug() << newService->library() + QLatin1String(".*");
        foreach (QString libPath, QCoreApplication::libraryPaths()) {
	    qDebug() << libPath;
            libPath += suffix;
            const QDir dir(libPath);
            foreach (const QString &pluginName, dir.entryList(libFilter, QDir::Files)) {
		
                QPluginLoader pluginLoader(libPath + pluginName);
                backend = (BackendInterface *)pluginLoader.instance();
                if (backend) {
                    break;
                }
            }
            if (backend) {
                break;
            }
        }
    }

    if (0 == backend) {
    /*    kError(600) << "Can not create backend object from factory for " <<
            newService->name() << ", " << newService->library() << ":\n" << errorReason;

        KMessageBox::error(0,
                i18n("<qt>Unable to use the <b>%1</b> Wormhole Backend:<br/>%2</qt>",
                    newService->name(), errorReason));*/
        return 0;
    }

    kDebug() << "using backend: " << newService->name();
qDebug() << backend->name();
  
    return backend;
}
void SnapshotManager::loadBackends()
{
    const KService::List offers = KServiceTypeTrader::self()->query("WormholeBackend",
            "Type == 'Service'");
    if (offers.isEmpty()) {
	  qDebug() << "no backend available!!!";   
        return;
    }

    qDebug() << "found " << offers.size() << " backends";


    for(int i=0; i<offers.size(); i++){
      qDebug() << i+1 << ". " << offers.at(i)->name();
      BackendInterface *backend = createBackend(offers.at(i));
        if (backend) {
            m_backendhash[backend->name()]=backend;
        }
    }
    /*KService::List::const_iterator it = offers.begin();
    const KService::List::const_iterator end = offers.end();
    while (it != end) {
	qDebug() << (*it)->name();
       QObject *backend = createBackend(*it);
        if (backend) {
            return backend;
        }
        ++it;
    }*/
    return;
}
